package com.ahs.service.generate;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import org.junit.jupiter.api.Test;
import org.redisson.api.RSet;
import org.redisson.api.RSetCache;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.TreeMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * @author riven zhang
 * @since 2021/8/23 18:00
 */
@SpringBootTest
@Slf4j
public class GenerateNo {

    private static final String DAY_DATE_FORMAT_PATTERN = "yyyyMMdd";
    private static final FastDateFormat FAST_DATE_FORMAT_DAY = FastDateFormat.getInstance(DAY_DATE_FORMAT_PATTERN);
    private static final String YC = "YC";
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private RedissonClient client;


    private String getNo() {
        // 以异常单前缀和当天的日期作为key,YC20210824
        String key = YC + FAST_DATE_FORMAT_DAY.format(System.currentTimeMillis());
        // 若当天key不存在则生成key并设置时间为24小时
        if (!redisTemplate.hasKey(key)) {
            redisTemplate.opsForSet().add(key, "0");
            redisTemplate.expire(key, 24, TimeUnit.SECONDS);
        }
        Random random = new Random();
        long add;
        int number;
        // 循环插入随机数，直到成功
        do {
            number = random.nextInt(9000) + 1000;
            add = redisTemplate.opsForSet().add(key, String.valueOf(number));
        } while (add < 1);
        return key + number;
    }


    @Test
    void test() throws ExecutionException, InterruptedException {
        RSetCache<Integer> fuck = client.getSetCache("fuck9527");
        System.out.println(fuck.size());
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 84865);
        System.out.println(fuck.size());
        boolean b = fuck.addAll(list);
        System.out.println("b = " + b);

        System.out.println(fuck.clearExpire());

    }

    @Test
    void timeTest() {

        TreeMap<Integer, Integer> map = new TreeMap<>();
        System.out.println(map.isEmpty());
        map.put(4, 1);
        map.put(1, 5);
        map.put(2, 3);
        map.put(3, 7);
        System.out.println(map.lastEntry().getValue());


    }

    @Test
    void get() {
        System.out.println(fillZeroFront(545454, 4));
    }


    private String geneExpNo() {
        // 以异常单前缀和当天的日期作为key,YC20210824
        String key = YC + FAST_DATE_FORMAT_DAY.format(System.currentTimeMillis());
        // 若当天key不存在则生成key并设置时间为24小时
        RSet<Integer> set = client.getSet(key);
        if (!set.isExists()) {
            set.add(0);
            set.expire(24L, TimeUnit.SECONDS);
        }
        while (true) {
            int i = RandomUtils.nextInt(1000, 10000);
            if (set.add(i)) {
                if (i > 5000) {
                    return key + i;
                }
            }
            if (set.size() >= 9000) {
                throw new RuntimeException("Full Keys!");
            }
        }
    }

    private String fillZeroFront(int num, int len) {
        StringBuilder sb = new StringBuilder(String.valueOf(num));
        int size = sb.length();
        if (size >= len) {
            return sb.toString();
        }
        for (int i = 0; i < len - size; i++) {
            sb.insert(0, 0);
        }
        return sb.toString();
    }


}
